package com.mdwz.sunnyradio;

import com.spoledge.aacplayer.AACFileChunkPlayer;
import com.spoledge.aacplayer.AACPlayer;
import com.spoledge.aacplayer.ArrayAACPlayer;
import com.spoledge.aacplayer.ArrayDecoder;
import com.spoledge.aacplayer.Decoder;
import com.spoledge.aacplayer.PlayerCallback;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

public class Radio extends Activity implements View.OnClickListener, PlayerCallback {
	
	private AACPlayer aacPlayer;
    private AACFileChunkPlayer aacFileChunkPlayer;
    private Button btplay;
    private Button btstop;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_radio);
		
		 btplay = (Button)findViewById(R.id.play_r);
	     btstop = (Button)findViewById(R.id.stop_r);
	     
	     btplay.setOnClickListener(this);
	     btstop.setOnClickListener(this);
	     
	     btplay.setEnabled(true);
         btstop.setEnabled(false);
         
         uiHandler = new Handler();
	     
	}
	
	private void start() {
        stop();
        aacPlayer = new ArrayAACPlayer( ArrayDecoder.create( Decoder.DECODER_FFMPEG_WMA ), this , 1500, 700);
        aacPlayer.playAsync( "mms://bcr.media.hinet.net/RA000064" );
    }


    private void stop() {
        if (aacFileChunkPlayer != null) { aacFileChunkPlayer.stop(); aacFileChunkPlayer = null; }
        if (aacPlayer != null) { aacPlayer.stop(); aacPlayer = null; }
    }

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.radio, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	private boolean bStart = false;
	private Handler uiHandler;

	@Override
	public void playerStarted() {
		// TODO Auto-generated method stub
		
		uiHandler.post( new Runnable() {
            public void run() {
            	bStart = true;
            	btplay.setEnabled(false);
            	btstop.setEnabled(true);
            }
        });
		
	}

	@Override
	public void playerPCMFeedBuffer(boolean isPlaying, int audioBufferSizeMs,
			int audioBufferCapacityMs) {
		// TODO Auto-generated method stub
		
		uiHandler.post( new Runnable() {
            public void run() {
              
            }
        });
		
	}

	@Override
	public void playerStopped(int perf) {
		// TODO Auto-generated method stub
		
		uiHandler.post( new Runnable() {
            public void run() {
            	bStart = false;
            	btplay.setEnabled(true);
            	btstop.setEnabled(false);
            }
        });
		
	}

	@Override
	public void playerException(Throwable t) {
		// TODO Auto-generated method stub
		
		 uiHandler.post( new Runnable() {
	            public void run() {
	                if (bStart) playerStopped( 0 );
	            }
	        });
		
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
		switch ( v.getId() ){
		case R.id.play_r:{
			start();
			break;
		}
			
		case R.id.stop_r:{
			stop();
            break;
		}
		}
		
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		stop();
	}
}
